package com.zgroup.common;

import magick.CompositeOperator;
import magick.DrawInfo;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
import magick.PixelPacket;


public class ImageUtils {

    public static void resize(String picFrom, String picTo, int width, int height) throws MagickException {
        // Resize
        ImageInfo info = new ImageInfo(picFrom);
        MagickImage image = new MagickImage(new ImageInfo(picFrom));
        MagickImage scaled = image.scaleImage(width, height);//小图片文件的大小.
        scaled.setFileName(picTo);
        scaled.writeImage(info);
    }

    /**
     * @param filePath
     * @param text
     * @param opacity  透明度 0 - 100, 0 为不透明
     * @return
     */
    public static String addTextToPicture(String filePath, String text, int opacity) {
        int idx = filePath.indexOf('.');
//		String newFileName = filePath.substring(0, idx) + "_twm"
//				+ filePath.substring(idx);
        String newFileName = filePath;

        try {
            ImageInfo info = new ImageInfo(filePath);

//			if (filePath.toUpperCase().endsWith("JPG")
//					|| filePath.toUpperCase().endsWith("JPEG")) {
//				info.setCompression(CompressionType.JPEGCompression); // 压缩类别为JPEG格式
//				info.setPreviewType(PreviewType.JPEGPreview); // 预览格式为JPEG格式
//				
//			}

            info.setQuality(95);
            MagickImage aImage = new MagickImage(info);

            DrawInfo aInfo = new DrawInfo(info);

            aInfo.setFill(PixelPacket.queryColorDatabase("green"));
            aInfo.setUnderColor(PixelPacket.queryColorDatabase("yellow"));
            aInfo.setOpacity(0);
            aInfo.setPointsize(20);

            // aInfo.setFont("Arial"); // 英文使用此字体也可

            // 注意这里解决中文字体问题，有以下两行才可正常显示
            String fontPath = "/home/user1/wmfont/simsun.ttf";
            aInfo.setFont(fontPath);
            aInfo.setTextAntialias(true);

            // Step 3: Writing The Text
            aInfo.setText(text);
            aInfo.setGeometry("+40+60");
            /*
                * if(width > height){ aInfo.setGeometry("+"
                * +(480-140)+"+"+(int)((480/p)-10)); // 加文字水印在图象上的位置 } else{
                * aInfo.setGeometry("+"+(int)((480*p)-140)+"+" +(480-10));
                * //加文字水印在图象上的位置 }
                */

            // Step 4: Annotating
            aImage.annotateImage(aInfo);

            // Step 5: Writing the new file
            aImage.setFileName(newFileName);
            aImage.writeImage(info);
            aImage.destroyImages();

            aImage = null;

        } catch (MagickException e) {

        }

        return newFileName;
    }

//	public static void waterMark(String picture,String waterMark, int opacity){
//		ImageInfo info=null;
//		MagickImage image=null;
//		MagickImage mask=null;
//		MagickImage dest=null;
//		try{
//			info=new ImageInfo();
//			image=new MagickImage(new ImageInfo(picture));
//			mask=new MagickImage(new ImageInfo(waterMark));
//			image.setFileName(picture);
//			image.writeImage(info);
//			dest=new MagickImage(new ImageInfo(picture));
//			dest.compositeImage(CompositeOperator.AtopCompositeOp,mask,1,10);
//			dest.setFileName(picture);
//			dest.writeImage(info);
//		}catch (MagickException e) {
//
//		}
//	}

    public static void waterMark(String logoPath, String srcPathName, String destPathName) {
        ImageInfo info = null;
        MagickImage image = null;
        MagickImage mask = null;
        MagickImage dest = null;
        try {
            info = new ImageInfo();
            image = new MagickImage(new ImageInfo(srcPathName + "[0]"));
            mask = new MagickImage(new ImageInfo(logoPath));
            image.setFileName(destPathName);
            image.writeImage(info);
            dest = new MagickImage(new ImageInfo(destPathName));
            dest.compositeImage(CompositeOperator.AtopCompositeOp, mask, 1, 1);
            dest.setFileName(destPathName);
            dest.writeImage(info);
        } catch (MagickException e) {
            e.printStackTrace();
        } finally {
            if (image != null) {
                image.destroyImages();
            }
            if (mask != null) {
                mask.destroyImages();
            }
            if (dest != null) {
                dest.destroyImages();
            }
        }
    }
}
